///: Replication script for: Measuring Ideological Polaization on the Circuit Courts of Appeals 1953-2022 
///: Authors: Alex Badas
///: Journal: Journal of Law and Courts 

///: Start log file
log using "BadasCCAPolarization.log" 

///: Open Circuit Courts of Appeals rosters
use "CourtsAppealsRoster.dta"

///: ER is a required package to run this replication file. Install ER. 
///: The ER package estimates the Esteban and Ray (1994) model of polarization

ssc install er 

///: This is a loop that estimates ideological polarization for the 1-10 Circuit Courts of Appeals between 1953 and 2022
forval j =1(1)10 {
forval i = 1953(1)2022  {
er JCS if year==`i' & circuit==`j' , n(none) a(0) no
	return list 
	gen Polarization`i'`j' = r(er_1)
}
}


///: This is a loop that estimates ideoloical polarization for the 11th Circuit Court of Appeals between 1981 and 2022
forval i = 1981(1)2022  {
er JCS if year==`i' & circuit==11 , n(none) a(0) no
	return list 
	gen Polarization`i'11 = r(er_1)
}



///: This is a loop that estimates ideological polarization for the DC Circuit Court of Appeals between 1981-2022
forval i = 1953(1)2022  {
er JCS if year==`i' & circuit==12 , n(none) a(0) no
	return list 
	gen Polarization`i'12 = r(er_1)
}


clear 

///: Load polarization scores. 
use "PolarizationScores.dta"

///: Generate Figure 1: Polarization Statistic by Circuit and Year
 twoway (line Polarization_Statistic Year), by(Circuit)
 
 
///: Estimate linear model used to show average polarization by Year (Figure 2) and Circuit (Figure 3)

regress Polarization_Statistic i.Year i.Circuit

///: Figure 2

margins Year, plot

///: Figure 3

margins Circuit, plot

///: Estimates linear model used to show unified delegations are less polarized. Table 1 and Figure 4. 

regress Polarization_Statistic i.UnifiedDelegation i.DC, vce(cluster Year)

margins UnifiedDelegation, plot

clear 

///: Load 

use "IDB.dta"

///: Regressions for Table 2

///: Days to Opinion Table 2, Column 1. 
nbreg OpinionDays Polarization USA pauper NumberDecisions NumberJudges   affirmed i.Circuit i.Year

///: Generate figure 5; left panel 
sum Polarization if OpinionDays !=. & Year !=.
return list
margins, at(Polarization=(`r(min)'(.02)`r(max)'))
marginsplot, title(Consequences of Polarization: Days to Judgement) plot1opts(color(black)msymbol(none) lwidth(medthick) ///
xtitle(Polarization Statistic) ytitle("Predicted Days" "to Judgement")) ///
recastci(rarea)  ///
ciopts(color(gs10%85)alwidth(none)) ///
addplot(hist Polarization_Statistic if OpinionDays !=., fcolor(gs1%30) lwidth(none) ///
percent ///
yaxis(2) ///
yscale(alt lcolor(gs10) axis(2)) ///
ylabel(0 "0%" 10 "" 20 ""  40 " "  , /// 
labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small)) /// 
ytitle(" ", axis(2)) /// 
xlabel(.068(.1).569) ///
legend(off))



///: Case received a full hearing with oral argument Table 2, Column 2. 
logit FullHearing Polarization USAparty pauper NumberDecisions NumberJudges  affirmed i.Circuit i.Year

///: Generate Figure 5; Center panel 
sum Polarization if FullHearing !=. & Year !=.
return list
margins, at(Polarization=(`r(min)'(.02)`r(max)'))

marginsplot, title(Consequences of Polarization: Full Hearing with Oral Argument) plot1opts(color(black)msymbol(none) lwidth(medthick) ///
xtitle(Polarization Statistic) ytitle("Predicted Probability" "Full Hearing")) ///
recastci(rarea)  ///
ciopts(color(gs10%85)alwidth(none)) ///
addplot(hist Polarization if FullHearing ! =., fcolor(gs1%30) lwidth(none) ///
percent ///
yaxis(2) ///
yscale(alt lcolor(gs10) axis(2)) ///
ylabel(0 "0%" 10 "" 20 ""  40 " "  , /// 
labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small)) /// 
ytitle(" ", axis(2)) /// 
xlabel(.069(.1).569) ///
legend(off))


///: Decision was published. Table 2, Column 3. 
logit Published Polarization USA pauper NumberDecisions NumberJudges  affirmed i.Circuit i.Year


///: Generate Figure 5; Right panel 
sum Polarization if Published !=. & Year !=.
return list
margins, at(Polarization=(`r(min)'(.02)`r(max)'))

marginsplot, title(Consequences of Polarization: Full Hearing with Oral Argument) plot1opts(color(black)msymbol(none) lwidth(medthick) ///
xtitle(Polarization Statistic) ytitle("Predicted Probability" "Published Signed Reasoned Opinion")) ///
recastci(rarea)  ///
ciopts(color(gs10%85)alwidth(none)) ///
addplot(hist Polarization if Published !=., fcolor(gs1%30) lwidth(none) ///
percent ///
yaxis(2) ///
yscale(alt lcolor(gs10) axis(2)) ///
ylabel(0 "0%" 05 "5%" 10 "10%" 30  " "  , /// 
labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small)) /// 
ytitle(" ", axis(2)) /// 
xlabel(.069(.1).569) ///
legend(off))

clear 


log close